package cn.wjdiankong.levideo.activity;

import com.ss.android.common.applog.GlobalContext;
import com.umeng.analytics.MobclickAgent;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.dialog.LoginDialog;
import cn.wjdiankong.levideo.utils.Const;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.StatusBarUtils;
import cn.wjdiankong.levideo.utils.Utils;

public class BaseActivity extends FragmentActivity{

	private View title;
	private TextView titleTxt;
	private ImageView leftImg, rightImg;
	
	protected boolean isGestureFinish = true;
	private GestureDetector gestureDetector;//要想使用手势滑动，就必须要这个GestureDetector对象  

	protected void initView(){
		StatusBarUtils.setWindowStatusBarFullScreen(this, Color.TRANSPARENT);
		initTitle();
	}

	protected void initData(){
		Utils.width = Utils.getDeviceWidth(this);
		Utils.height = Utils.getDeviceHeight(this);
		/* 此处需要gestureDetector对象实例化*/
        gestureDetector = new GestureDetector(this, onGestureListener); 
	}

	private void initTitle(){
		title = findViewById(R.id.result_title);
		title.setBackgroundColor(Const.theme_bg);
		titleTxt = (TextView)findViewById(R.id.title);
		leftImg = (ImageView)findViewById(R.id.left_img);
		rightImg = (ImageView)findViewById(R.id.right_img);
		leftImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				clickLeftImg();
			}
		});
		rightImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				clickRightImg();
			}
		});
		titleTxt.setVisibility(View.GONE);
		leftImg.setVisibility(View.GONE);
		rightImg.setVisibility(View.GONE);
	}

	protected void setTitle(String str){
		if(titleTxt != null){
			titleTxt.setText(str);
			titleTxt.setVisibility(View.VISIBLE);
		}
	}

	protected void setLeftImg(int resId){
		if(leftImg != null){
			leftImg.setBackgroundResource(resId);
			leftImg.setVisibility(View.VISIBLE);
		}
	}

	protected void setRightImg(int resId){
		if(rightImg != null){
			rightImg.setBackgroundResource(resId);
			rightImg.setVisibility(View.VISIBLE);
		}
	}

	@SuppressLint("InflateParams")
	protected void showLoginDialog(){
		LoginDialog dialog = new LoginDialog(this);
		dialog.show();
	}

	/** 
	 * 在此实例化OnGestureListener监听的实例 
	 */  
	private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {  
		@Override  
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
				float velocityY) {  
			//e1就是初始状态的MotionEvent对象，e2就是滑动了过后的MotionEvent对象  
			//velocityX和velocityY就是滑动的速率  
			float x = e2.getX() - e1.getX();//滑动后的x值减去滑动前的x值 就是滑动的横向水平距离(x)  
			//如果滑动的横向距离大于100，表明是右滑了，那么就执行下面的方法，可以是关闭当前的activity
			if (x > 100) {  
				finish();
			}  
			//如果滑动的横向距离大于100，表明是左滑了(因为左滑为负数，所以距离大于100就是x值小于-100)  
			if (x < -100) {  
			}  
			return true;  
		}  
	};
	
	/** 
     * 将MotionEvent事件处理交给gestureDetector对象 
     */  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {
    	Logger.log("is getstrue:"+isGestureFinish);
    	if(isGestureFinish){
    		return gestureDetector.onTouchEvent(event);
    	}
    	return super.onTouchEvent(event);
    }  

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(getPageName());
		MobclickAgent.onResume(GlobalContext.getContext());
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(getPageName());
		MobclickAgent.onPause(GlobalContext.getContext());
	}

	protected void clickLeftImg(){};
	protected void clickRightImg(){};
	protected void clickRightImg1(){};

	protected String getPageName() {
		return "defalut";
	}

}
